<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/10/31
 * Time: 11:05
 */
namespace app\api\controller;
use think\Request;
use think\Log;
use think\db;
require_once BASE_ROOT."core/application/api/controller/BaseController.php";
require_once BASE_ROOT."core/application/api/service/ArlikiService.php";
use app\api\service\OrderService;
use app\api\service\ArlikiService;
class Order extends BaseController
{
    public function PrintTest(){
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $oid = Request::instance()->param('order_id');
        $order  = new OrderService();
        $info=$order->Print_test($mch_id,$oid);
        return $info;
    }
    /**
     * 创建订单
     * @return string
     */
    public function CreateOrder()
    {
        $rs = array('code' => 0,'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        //订单数据
        $data = [
            'order_no'  => $this->createOrderNo(),  //订单号
            'out_trade_no' => $this->createOutTradeNo(),    //支付流水号
            'pay_type' => Request::instance()->param('pay_type','1'),   //支付类型
            'shipping_type' => Request::instance()->param('shipping_type','1'), //物流类型
            'shipping_money' => Request::instance()->param('shipping_money',0), //物流价格
            'shipping_company_id' => Request::instance()->param('shipping_company_id',0),   //物流公司
            'buyer_message' => Request::instance()->param('buyer_message',''),  //买家留言
            'receiver_zip' => Request::instance()->param('receiver_zip'),   //收货邮编
            'discount_money' => Request::instance()->param('discount_money','0.00'),   //优惠价格
            'coupon_id' => Request::instance()->param('coupon_id',0),   //优惠劵id
            'coupon_money' => Request::instance()->param('coupon_money','0.00'),//优惠劵
            'rebate_money' => Request::instance()->param('rebate_money','0.00'),//会员折扣
            'activity_order_type'=>Request::instance()->param('activity_order_type',0),
            'create_time' => time(),
            //必填值
            'buyer_id' => Request::instance()->param('buyer_id'),   //购买人id
            'user_name' => Request::instance()->param('user_name'), //购买人名称
            'receiver_mobile' => Request::instance()->param('phone'), //电话
            'receiver_area' => Request::instance()->param('area',0), //地区
            'receiver_address' => Request::instance()->param('address',0),    //详细地址
            'receiver_name' => Request::instance()->param('consigner'),     //收件人
            'order_money' => Request::instance()->param('order_money'),     //订单价格
           // 'pay_money' => Request::instance()->param('pay_money'),         //实付价格
            'sku_id' => Request::instance()->param('sku_id'),   //实例: sku_id:num,sku_id1:num1
            'mch_id'  => $mch_id,
            'mailing_type'=>Request::instance()->param('mailing_type',1),
            'mention_time'=>Request::instance()->param('mention_time'),
            'jf_money'=>Request::instance()->param('jf_money','0.00'),
        ];
        $jf=Request::instance()->param('need_jf',0);
        if($jf>0&&$data['mailing_type']!=1){
            //Db::name('ybsc_user')->where(['uid'=>$data['buyer_id']])->setDec('integral',intval($jf));
            $user=Db::name('ybsc_user')->where(['uid'=>$data['buyer_id']])->find();
            Db::name('ybsc_user')->where(['uid'=>$data['buyer_id']])->setDec('integral',$jf);
            Db::name('ybsc_integral_detail')->insert([
                'itype'=>2,
                'ctype'=>1,
                'integral_total'=>$user['integral'],
                'integral'=>$jf,
                'consume'=>0,
                'time'=>time(),
                'explain'=>'积分抵扣',
                'user_id'=>$data['buyer_id'],
            'mch_id'=>$data['mch_id'],
            ]);

        }
        $data['pay_money']=round((floatval($data['order_money'])-floatval($data['rebate_money'])+floatval($data['shipping_money'])-floatval($data['coupon_money'])-floatval($data['discount_money'])-floatval($data['jf_money'])),2);
        if(floatval($data['pay_money'])<0){
            $data['pay_money']=0;
        }
        if($data['discount_money'] != "0.00" && $data['coupon_money'] != "0.00")
        {
            $data['activity_order_type'] = 4;
        }
        $rule = [
            ['buyer_id','require|number'],
            ['user_name','require'],
            ['mailing_type','require'],
            ['receiver_mobile','require|number|length:11'],
            //['receiver_area','require|number'],
            //['receiver_address','require'],
            ['receiver_name','require'],
            ['order_money','require|number'],
            ['pay_money','require|number'],
            ['sku_id','require'],
            ['mch_id','require','不存在商户'],
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->createOrder($data);
//        if(empty($info))
//        {
//            $rs['code'] = 1;
//            $rs['msg'] = "生成订单失败";
//            return json_encode($rs);
//        }
        if($info['err_code']==0)
        {
            $ser=new ArlikiService($mch_id);
            $ser->send_sms($data["order_no"]);
            $rs['info'] = $info['info'];
            return json_encode($rs);
        }else{
            $rs['code'] = 1;
            $rs['msg'] = $info['msg'];
            return json_encode($rs);
        }
    }
    /**
     * 用户订单列表
     * @return string
     */
    public function OrderList()
    {
        $rs = array('code' => 0,'info' => array());
        $uid = Request::instance()->param('uid');   //用户id
        $status = Request::instance()->param('status',null);  //订单状态1
        $page = Request::instance()->param('page',1);
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $rule = [
            ['buyer_id','require','uid不能为空'],
            ['mch_id','require','不存在商户'],
        ];
        $data = [
            'buyer_id'  => $uid,
            'mch_id'  => $mch_id,
        ];
        if($status != null)
        {
            if($status == 4)
            {
                $data['order_status'] = ['in',[4,5]];
            }
            else
            {
                $data['order_status'] = $status;
            }
        }
        else
        {
            $data['order_status'] = ['<>','-1'];
        }
//        $data = array_filter($data);
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->orderList($data,$page);
        $rs['info'] = $info;
        return json_encode($rs);
    }
    /**
     * 用户单条订单信息
     * @return string
     */
    public function GetOrder()
    {
        $rs = array('code' => 0,'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $rule = [
            ['buyer_id','require','uid不能为空'],
        ];
        $data = [
            'buyer_id'  => Request::instance()->param('buyer_id'),  //购买人id
            'order_id'  => Request::instance()->param('order_id'),  //订单id
            'mch_id'  => $mch_id,
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->getOrder($data);
        $rs['info'] = $info;
        return json_encode($rs);
    }
    /**
     * 用户签收订单
     * @return string
     */
    public function SignOrder()
    {
        //$rs = array('code' => 0,'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $rule = [
            ['buyer_id','require'],
            ['order_id','require'],
            ['mch_id','require']
        ];
        $data = [
            'buyer_id'  => Request::instance()->param('buyer_id'),      //购买人id
            'order_id'  => Request::instance()->param('order_id'),      //订单id
            'order_status'  => 2,
            'mch_id'=>$mch_id
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->signOrder($data);
       // $rs['info'] = $info;
        return $info;
    }
    /**
     * 用户取消订单
     * @return string
     */
    public function CancelOrder()
    {
        $rs = array('code' => 0,'info' => array());
        $rule = [
            ['buyer_id','require'],
            ['order_id','require'],
        ];
        $data = [
            'buyer_id'  => Request::instance()->param('buyer_id'),//购买人id
            'order_id'  => Request::instance()->param('order_id'),//订单id
            'order_status'  => 0,
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->cancelOrder($data);
        if(empty($info))
        {
            $rs['code'] = 1;
            $rs['msg'] = "订单取消失败";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }
    /**
     * 订单删除
     * @return string
     */
    public function DelOrder()
    {
        $rs = array('code' => 0,'info' => array());
        $rule = [
            ['buyer_id','require'],
            ['order_id','require'],
        ];
        $data = [
            'buyer_id'  => Request::instance()->param('buyer_id'),//订单id
            'order_id'  => Request::instance()->param('order_id'),//订单id
            'order_status' => ['in',[0,-1,3,5]],
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->delOrder($data);
        if(empty($info))
        {
            $rs['code'] = 1;
            $rs['msg'] = "订单删除失败";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }
    /**
     * 用户退款
     * @return string
     */
    public function RefundOrder()
    {
        $rs = array('code' => 0,'info' => array());
        $rule = [
            ['buyer_id','require'],
            ['order_id','require'],
        ];
        $data = [
            'buyer_id'  => Request::instance()->param('buyer_id'),      //购买人id
            'order_id'  => Request::instance()->param('order_id'),      //订单id
            'order_status'  => ['in',[1,2,3]],
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $order  = new OrderService();
        $info = $order->refundOrder($data);
        if(empty($info))
        {
            $rs['code'] = 1;
            $rs['msg'] = "订单退款失败";
            return json_encode($rs);
        }
        $rs['info'] = $info;
        return json_encode($rs);
    }
}
